package cn.lyjuan.aliyun.mq.conf.java.conf;

import cn.lyjuan.base.util.StringUtils;

import java.util.Properties;

/**
 * 消息队列使用配置
 * Created by chad on 2016/9/30.
 */
public class MQUserCst
{
    public static final String ALL_TAG = "*";

    /**
     * 实例类型
     */
    public enum UserType
    {
        PRODUCER("P"),
        CONSUMER("C");

        private String value;

        UserType(String value)
        {
            this.value = value;
        }

        @Override
        public String toString()
        {
            return this.value;
        }

        public static UserType parseUserType(String str)
        {
            if (PRODUCER.toString().equalsIgnoreCase(str)) return PRODUCER;

            else if (CONSUMER.toString().equalsIgnoreCase(str)) return CONSUMER;

            return null;
        }
    }

    /**
     * 主题
     */
    private String topicId;

    /**
     * 是否启用生产者或消费者
     */
    private boolean isStart;

    /**
     * 标识是生产者（P）还是消费者(C)
     */
    private UserType userType;

    /**
     * 生产者或消费者ID
     */
    private String userId;

    /**
     * 生产者生产的或消费者消费的消息标签
     */
    private String msgTag;

    /**
     * 消费者线程启动数量
     */
    private int consumeThreadNums;


    public MQUserCst(String topicId, boolean isStart, UserType userType, String userId, String msgTag, String consumeThreadNums)
    {
        this.topicId = topicId;
        this.userType = userType;
        this.isStart = isStart;
        this.userId = userId;
        this.msgTag = msgTag;
        if (StringUtils.isNumber(consumeThreadNums))
            this.consumeThreadNums = Integer.parseInt(consumeThreadNums);
        else// 默认为1
            this.consumeThreadNums = 1;
    }

    public MQUserCst(Properties pro)
    {
        this(pro.getProperty("mq.topic.id"),
                Boolean.parseBoolean(pro.getProperty("mq.start")),
                UserType.parseUserType(pro.getProperty("mq.user.type")),
                pro.getProperty("mq.user.id"),
                pro.getProperty("mq.msg.tag"),
                pro.getProperty("mq.user.nums", "1"));
    }

    @Override
    public String toString()
    {
        return "MQUserCst{" +
                "topicId='" + topicId + '\'' +
                ", isStart=" + isStart +
                ", userType=" + userType +
                ", userId='" + userId + '\'' +
                ", msgTag='" + msgTag + '\'' +
                ", consumeThreadNums=" + consumeThreadNums +
                '}';
    }

    public String getTopicId()
    {
        return topicId;
    }

    public void setTopicId(String topicId)
    {
        this.topicId = topicId;
    }

    public boolean isStart()
    {
        return isStart;
    }

    public void setStart(boolean start)
    {
        isStart = start;
    }

    public String getUserId()
    {
        return userId;
    }

    public void setUserId(String userId)
    {
        this.userId = userId;
    }

    public String getMsgTag()
    {
        return msgTag;
    }

    public void setMsgTag(String msgTag)
    {
        this.msgTag = msgTag;
    }

    public UserType getUserType()
    {
        return userType;
    }

    public void setUserType(UserType userType)
    {
        this.userType = userType;
    }

    public static String getAllTag()
    {
        return ALL_TAG;
    }

    public int getConsumeThreadNums()
    {
        return consumeThreadNums;
    }

    public void setConsumeThreadNums(int consumeThreadNums)
    {
        this.consumeThreadNums = consumeThreadNums;
    }
}
